package com.example.attrimirror.data;

import android.content.Context;
import android.widget.Toast;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class CollectionInitializer implements Initializer {
    private final Gson gson;

    public CollectionInitializer() {
        this.gson = new GsonBuilder().setPrettyPrinting().create();
    }

    @Override
    public void initialize(Context context, File dataDir) {
        File collectionFile = new File(dataDir, "collection.json");
        try {
            if (!collectionFile.exists()) {
                if (!collectionFile.createNewFile()) {
                    Toast.makeText(context, "创建仓库数据文件失败", Toast.LENGTH_SHORT).show();
                    return;
                }
                try (FileWriter writer = new FileWriter(collectionFile)) {
                    Map<String, Object> data = new HashMap<>();
                    
                    // 添加物品列表
                    List<Map<String, Object>> items = new ArrayList<>();
                    
                    // 添加补签珠的数据
                    Map<String, Object> latecheckToken = new HashMap<>();
                    latecheckToken.put("id", "latechecktoken");
                    latecheckToken.put("name", "补签珠");
                    latecheckToken.put("number", 30);
                    latecheckToken.put("icon", "mon.png");
                    items.add(latecheckToken);

                    // 添加所有星期的签到勋章
                    String[] days = {"mon", "tue", "wed", "thu", "fri", "sat", "sun"};
                    String[] dayNames = {"一", "二", "三", "四", "五", "六", "日"};
                    
                    for (int i = 0; i < days.length; i++) {
                        Map<String, Object> medal = new HashMap<>();
                        medal.put("icon", days[i] + ".png");
                        medal.put("id", "checkin." + days[i]);
                        medal.put("name", "周" + dayNames[i] + "签到勋章");
                        medal.put("number", 0);
                        items.add(medal);
                    }
                    
                    data.put("items", items);
                    gson.toJson(data, writer);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(context, "创建仓库数据文件失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
} 